package cn.monkey.family.server.domain.uc.org.role;

import cn.monkey.family.data.uc.org.role.OrgRole;
import cn.monkey.family.data.uc.org.role.QOrgRole;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.querydsl.ListQuerydslPredicateExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.ListQueryByExampleExecutor;

import java.util.Collection;
import java.util.Optional;

@NoRepositoryBean
public interface OrgRoleRepository extends ListQueryByExampleExecutor<OrgRole>, ListQuerydslPredicateExecutor<OrgRole>, CrudRepository<OrgRole, String> {

    default Collection<OrgRole> findByOrgId(String orgId) {
        OrgRole orgRole = new OrgRole();
        orgRole.setOrgId(orgId);
        return this.findAll(Example.of(orgRole, ExampleMatcher.matchingAll().withMatcher("orgId", ExampleMatcher.GenericPropertyMatchers.exact())));
    }
    default Optional<OrgRole> findByRoleId(String roleId) {
        OrgRole orgRole = new OrgRole();
        orgRole.setRoleId(roleId);
        return this.findOne(Example.of(orgRole, ExampleMatcher.matchingAll().withMatcher("roleId", ExampleMatcher.GenericPropertyMatchers.exact())));
    }

    default Collection<OrgRole> findAllByRoleIdIn(Collection<String> roleIds) {
        return this.findAll(QOrgRole.orgRole.roleId.in(roleIds));
    }
}
